﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Whoever.Framework.Reflection
{
    public class Helpers
    {
        public static Dictionary<PropertyInfo, object> GetAttributes(object entity, Type typeAtt)
        {
            return GetAttributes(entity.GetType(), typeAtt);
        }

        public static Dictionary<PropertyInfo, object> GetAttributes(Type typeEntity, Type typeAtt)
        {
            var attributes = new Dictionary<PropertyInfo, object>();
            foreach (PropertyInfo property in typeEntity.GetProperties())
            {
                var att = property.GetCustomAttributes(typeAtt, true).ToList();
                foreach (object o in att)
                {
                    attributes.Add(property, o);
                }
            }
            return attributes;
        }

        public static object GetPropertyWithAttribute(Type typeEntity, Type typeAtt)
        {
            return GetAttributes(typeEntity, typeAtt).Single().Key;
        }

        public static object GetAttributeFromType(PropertyInfo typeEntity, Type attributeType)
        {
            var atts = typeEntity.GetCustomAttributes(attributeType, true);
            return atts.First();
        }
    }
}
